package com.android.vgongyi.activity;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.android.vgongyi.R;
import com.android.vgongyi.common.AppApiPreference;
import com.android.vgongyi.dao.CommonDAO;
import com.android.vgongyi.entity.CommonEntity;
import com.android.vgongyi.entity.VersionEntity;
import com.android.vgongyi.service.VGongyiService;
import com.android.vgongyi.service.VGongyiUpdateService;
import com.android.vgongyi.utils.SystemUtil;
import com.mobclick.android.MobclickAgent;

public class WelcomeActivity extends Activity {
	
	public static boolean SDCARDISEXIST = true;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobclickAgent.onError(this);//UMeng UTIL
		setContentView(R.layout.welcome);
        
		getUMengParameters();
		
		Intent intent = new Intent();
		intent.setClass(this, VGongyiUpdateService.class);
		startService(intent);
		
		//SDCard 
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
			
			File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/VGongyi/imagefiles");
			if (!dir.exists())	dir.mkdirs();
			
		} else {

			SDCARDISEXIST = false;
			Toast.makeText(getApplicationContext(), "NO SDCARD!",Toast.LENGTH_SHORT).show();
			
		}
		
		if(!SystemUtil.checkNetwork(this)){
			Toast.makeText(this, R.string.network_unavailable, Toast.LENGTH_SHORT).show();
		}
		new Thread() {
			
			public void run() {
				
				CommonDAO commonDAO = new CommonDAO(WelcomeActivity.this);
				ArrayList<CommonEntity> list = commonDAO.query(null, null, null, null);
				VGongyiService service = new VGongyiService(getApplicationContext());
				if(list.size() == 0){
					service.getLatestCommonFeeds();//no cache data , get the latest data from server
				}
				
				VersionEntity versionEntity = service.getLatestVersion();
				Intent intent  = new Intent();
				if(versionEntity !=null){
					String latestVersion = getVersionName();
					if(!versionEntity.getVersion().equals(latestVersion)) {
						String versionUrl = versionEntity.getUrl();
						String versionTip = versionEntity.getTips();
						intent.putExtra("versionUrl", versionUrl);
						intent.putExtra("versionTip", versionTip);
					}
					
				}
				
				intent.setClass(WelcomeActivity.this,HomeActivity.class);
				WelcomeActivity.this.startActivity(intent);
				WelcomeActivity.this.finish();

			};
		}.start();

	}
    public void onResume() {
    	
    	super.onResume();
    	
    	MobclickAgent.onResume(this);
    
    }
    public void onPause() {
    
    	super.onPause();
    	MobclickAgent.onPause(this);
    
    }
    
	private String getVersionName(){
		
		String versionName = "";
		
		try {
			PackageInfo pi = this.getPackageManager().getPackageInfo(getPackageName(), 0);
			versionName = pi.versionName; 
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return versionName;		
		
	}
	
	private boolean mIsOnlineParamEmpty(String param){
		
		String str = MobclickAgent.getConfigParams(this,param);
		return TextUtils.isEmpty(str);
		
	}
	
	private void getUMengParameters(){
		
		MobclickAgent.updateOnlineConfig(this);
		
		AppApiPreference.SINA_APP_KEY 			 = mIsOnlineParamEmpty("sina_key")? AppApiPreference.SINA_APP_KEY : MobclickAgent.getConfigParams(this,"sina_key");
        AppApiPreference.SINA_APP_KEY_SECRET 	 = mIsOnlineParamEmpty("sina_secret")? AppApiPreference.SINA_APP_KEY_SECRET : MobclickAgent.getConfigParams(this, "sina_secret");
        AppApiPreference.TECENT_APP_KEY 		 = mIsOnlineParamEmpty("qq_key")?AppApiPreference.TECENT_APP_KEY:MobclickAgent.getConfigParams(this, "qq_key");
        AppApiPreference.TECENT_APP_KEY_SECRET   = mIsOnlineParamEmpty("qq_secret")?AppApiPreference.TECENT_APP_KEY_SECRET:MobclickAgent.getConfigParams(this, "qq_secret");
        AppApiPreference.WANGYI_APP_KEY 		 = mIsOnlineParamEmpty("wy_key")?AppApiPreference.WANGYI_APP_KEY:MobclickAgent.getConfigParams(this, "wy_key");
        AppApiPreference.WANGYI_APP_KEY_SECRET	 = mIsOnlineParamEmpty("wy_secret")?AppApiPreference.WANGYI_APP_KEY_SECRET:MobclickAgent.getConfigParams(this, "wy_secret");
        AppApiPreference.SOHO_APP_KEY 			 = mIsOnlineParamEmpty("sohu_key")?AppApiPreference.SOHO_APP_KEY:MobclickAgent.getConfigParams(this, "sohu_key");
        AppApiPreference.SOHO_APP_KEY_SECRET 	 = mIsOnlineParamEmpty("sohu_secret")?AppApiPreference.SOHO_APP_KEY_SECRET:MobclickAgent.getConfigParams(this, "sohu_secret");
       
        //AppApiPreference.COMMON_HEADER			 = "";
        AppApiPreference.ABOUT_URL				 = mIsOnlineParamEmpty("about_url")?AppApiPreference.ABOUT_URL:MobclickAgent.getConfigParams(this, "about_url");
        AppApiPreference.FAV_STATUS_URL	   		 = mIsOnlineParamEmpty("fav_status_url")?AppApiPreference.FAV_STATUS_URL:MobclickAgent.getConfigParams(this, "fav_status_url");
        AppApiPreference.AD_URL					 = mIsOnlineParamEmpty("ad_url")?AppApiPreference.AD_URL:MobclickAgent.getConfigParams(this, "ad_url");
		AppApiPreference.LOAD_STATUS_URL		 = mIsOnlineParamEmpty("load_status_url")?AppApiPreference.LOAD_STATUS_URL:MobclickAgent.getConfigParams(this, "load_status_url");
		
		AppApiPreference.RATING_URL				 = mIsOnlineParamEmpty("rating_url")?AppApiPreference.RATING_URL:MobclickAgent.getConfigParams(this, "rating_url");
		AppApiPreference.AD_ALWAYS_SHOW			 = mIsOnlineParamEmpty("ad_always_show")?AppApiPreference.AD_ALWAYS_SHOW:
			   (MobclickAgent.getConfigParams(this, "ad_always_show").trim().equals("YES")?true:false);
       
		AppApiPreference.AT_USER_SINA			 = mIsOnlineParamEmpty("at_user_sina")?AppApiPreference.AT_USER_SINA:MobclickAgent.getConfigParams(this, "at_user_sina");
        AppApiPreference.AT_USER_QQ				 = mIsOnlineParamEmpty("at_user_qq")?AppApiPreference.AT_USER_QQ:MobclickAgent.getConfigParams(this, "at_user_qq");
        AppApiPreference.AT_USER_SOHU			 = mIsOnlineParamEmpty("at_user_sohu")?AppApiPreference.AT_USER_SOHU:MobclickAgent.getConfigParams(this, "at_user_sohu");
        AppApiPreference.AT_USER_WY				 = mIsOnlineParamEmpty("at_user_wy")?AppApiPreference.AT_USER_WY:MobclickAgent.getConfigParams(this, "at_user_wy");
        AppApiPreference.LATEST_BLOG_URL 		 = mIsOnlineParamEmpty("latest_blogs_url")?AppApiPreference.LATEST_BLOG_URL:MobclickAgent.getConfigParams(this, "latest_blogs_url");

		
	}
    

}
